package cz.cvut.liy.data;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;



public class Test implements Serializable{

	private String name;
	private List<Question> questions;
	private LinkedList<Integer> notAnsweredQuestions;
	private boolean started;
	private int ansveredQuestions = 0;
	private static Test startTest;
	
	public static Test getStartTest() {
		return startTest;
	}
	
	public static void setStartTest(Test startTest) {
		Test.startTest = startTest;
	}
	
	
	public Test(String name) {
		this.name = name;
		questions = new ArrayList<Question>();
		notAnsweredQuestions = new LinkedList<Integer>();
	}
	
	public void initAnsweredQuestions(){
		notAnsweredQuestions = new LinkedList<Integer>();
		for (int i=0;i<questions.size();i++) notAnsweredQuestions.add(i);
	}
	
	public String getName() {
		return name;
	}
	
	public void addQuestion(Question question){
		questions.add(question);
	}
	
	public boolean isStarted() {
		return started;
	}
	
	public int getAnsveredQuestions() {
		if (notAnsweredQuestions.size() == 0) return 0;
		return questions.size() - notAnsweredQuestions.size();
	}
	
	public LinkedList<Integer> getNotAnsweredQuestion()
	{
		return notAnsweredQuestions;
	}
	
	public void setAnsveredQuestions(int ansveredQuestions) {
		this.ansveredQuestions = ansveredQuestions;
	}
	
	public void setStarted(boolean started) {
		this.started = started;
	}
	
	public List<Question> getQuestions() {
		return questions;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return name; 
	}
	
	public static void saveFile(List<Test> test, Context context){
		FileOutputStream fos;
		try {
			fos = context.openFileOutput("test", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(test);
			oos.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static List<Test> loadFile(Context context){
		try {
			FileInputStream fis = context.openFileInput("test");
		    ObjectInputStream ois = new ObjectInputStream(fis);
		    List<Test> test = (ArrayList<Test>) ois.readObject();
		    return test;
		} catch (Exception e) {
			return new ArrayList<Test>();
		}
	}
}
